﻿/*
(c) 2010 - Open Source Business Foundation e. V. (www.osbf.de)
This software is provided under Microsoft Reciprocal License (Ms-RL)
(http://www.microsoft.com/opensource/licenses.mspx#Ms-RL)
All other rights reserved
*/


using System.Collections.Generic;
using System.Data.Services.Providers;
using DataServiceProvider;
using MicroFormatProcessing;

namespace MicroFormatWebRole.OData {
    /// <summary>
    /// This class is the container for OData queried Tags
    /// </summary>

    public class MfTagDSPResourceList : MfDSPResourceList {

        private string UserId { get; set; }
        public DSPResource User {get;set;}

        
        public MfTagDSPResourceList(DSPContext context, DSPMetadata metadata, string userId) : base(context, metadata) {
            this.UserId = userId;
        }

        protected override void LoadData() {
            MicroFormatRepository microformatsRespository = new MicroFormatRepository();
            string[] tagPropertyList = { Constants.ODATA_COLLECTIONOWNER_USER, Constants.ODATA_FIELD_TAGNAME, Constants.ODATA_FIELD_USERID, Constants.ODATA_FIELD_ROWKEY, Constants.ODATA_COLLECTIONS_MICROFORMATS };
            TagRepository tagRepository = new TagRepository();
            ResourceSet tagSet;
            metadata.TryResolveResourceSet(Constants.ODATA_ENTITY_TAG, out  tagSet);

            foreach (Tag tag in tagRepository.FindByUserId(UserId)) {
                MfMicroformatDSPResourceList mfList = new MfMicroformatDSPResourceList(context, metadata, UserId,  tag.GetTagName());
                DSPResource tagResource = CreateResourceObject(tagSet.ResourceType, tagPropertyList, User, tag.GetTagName(), tag.GetUserId(), tag.RowKey, mfList);
                mfList.Tag = tagResource;
                mfList.User = User;
                Add(tagResource);
            }
        }

    }
}